package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysImg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysGood;
import com.ruoyi.project.system.service.ISysGoodService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author theme
 * @date 2021-01-17
 */
@Api(description = "商品Controller")
@RestController
@RequestMapping("/system/good")
public class SysGoodController extends BaseController {
    @Autowired
    private ISysGoodService sysGoodService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('system:good:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGood sysGood) {
        startPage();
        List<SysGood> list = sysGoodService.selectSysGoodList(sysGood);
        return getDataTable(list);
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('system:good:query')")
    @GetMapping(value = "/{goodId}")
    public AjaxResult getInfo(@PathVariable("goodId") Long goodId) {
        SysGood sysGood=sysGoodService.selectSysGoodById(goodId);
        List<SysImg> sysImgs=new ArrayList<>();
        if(StringUtils.isNotEmpty(sysGood.getPics())){
            String[] strs=sysGood.getPics().split(",");
            for(String str:strs){
                SysImg sysImg=new SysImg();
                sysImg.setUrl(str);
                sysImgs.add(sysImg);
            }
        }
        if(StringUtils.isNotEmpty(sysGood.getImage())){
            SysImg sysImg=new SysImg();
            sysImg.setUrl(sysGood.getImage());
            List<SysImg> imgs=new ArrayList<>();
            imgs.add(sysImg);
            sysGood.setFiles(imgs);
        }
        sysGood.setFileList(sysImgs);
        return AjaxResult.success(sysGood);
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('system:good:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGood sysGood) {
        if(sysGoodService.checkCodeUnique(sysGood.getCode())>0){
            return AjaxResult.error("商品编码已存在!");
        }
        sysGood.setCreateTime(new Date());
        sysGood.setStatus(1l);
        if(null!=sysGood.getFileList()&&!sysGood.getFileList().isEmpty()){
            sysGood.setPics(sysGood.getFileList().stream().map(SysImg::getUrl).collect(Collectors.joining(",")));
        }
       int flag= sysGoodService.insertSysGood(sysGood);
       /* if(flag>0){
            redisTemplate.opsForValue().set(sysGood.getGoodId()+"",sysGood.getStock());
        }*/
        return toAjax(flag);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('system:good:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGood sysGood) {
        if(null!=sysGood.getFileList()&&!sysGood.getFileList().isEmpty()){
            sysGood.setPics(sysGood.getFileList().stream().map(SysImg::getUrl).collect(Collectors.joining(",")));
        }
        int flag=sysGoodService.updateSysGood(sysGood);
      /*  if(flag>0){
            SysGood good=sysGoodService.selectSysGoodById(sysGood.getGoodId());
            redisTemplate.opsForValue().set(good.getGoodId()+"",good.getStock());
        }*/
        return toAjax(flag);
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('system:good:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodIds}")
    public AjaxResult remove(@PathVariable Long[] goodIds) {
        return toAjax(sysGoodService.deleteSysGoodByIds(goodIds));
    }
}
